*********************************************************************************
*                                                                               *
*                Election Frequency - Preparing the Dataset     	            *
*                                                                               *
*                         		  Indexes                                       *
*                                                                               *
*********************************************************************************
 
global beg_path `"C:\Users\JohnSmith\Dropbox\Replication_EF\"'   
 
cd `"${beg_path}Data\Analysis 1\"'
use electionsdata_3.dta, clear

*\ National Elections (1st post-war & democratic) \*

***** Ranked elecetions

* creating groups
sort Country Type date

* Creating dummy for national elections
gen filter_el = 1
replace filter_el = 0 if Nondem == 1
replace filter_el = 0 if Country == 5 & Year == 1964 & Type == 2 // 1964 election in Greece, not included in our study

egen Groupleg = group(Type) if Type==2&filter_el==1&date>-5202&Round==1

* Ranking within country
sort Country Groupleg date 		 
quietly by Country Groupleg :  gen Rankleg = cond(_N==1,0,_n) if Groupleg == 1
sum Rankleg // max == 28 (Denmark)

**** Variables with national elections dates 

*To avoid problems with negative dates (before 1960), we will add a constant equal to the miminal date in the dataset
gen posit_date = date 
su date 
replace posit_date = date+abs(r(min)) // all dates are positive

sort Country posit_date 		 
forvalues i=1(1)28  { 
by Country : generate Dateleg`i' = posit_date if `i' == Rankleg
bysort Country (Dateleg`i') : gen Dateleg_`i' = Dateleg`i'[1]
drop Dateleg`i'
}


**** Time to legislative election
forvalues i=1(1)28  { 
generate Timetoleg_`i' = Dateleg_`i' - posit_date if posit_date < Dateleg_`i'
}


*** Coding Snap elections (considered when election took place at least 0.5 years ahead of schedule and not first dem. elections) *** 
sort Country Rankleg Round
gen time_since_last_leg = posit_date-posit_date[_n-1] if Country==Country[_n-1] & Round==Round[_n-1] & Type==2 & Type[_n-1]==2
gen snap = .
replace snap = 0 if Rankleg !=.
replace snap = 1 if Type==2 & Country == 1 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5) // Denmark, regular term 4 years throughout 
replace snap = 1 if Type==2 & Country == 2 & Rankleg>2 & Rankleg!=. & time_since_last_leg < (365.25*3.5) // Spain, regular term 4 years throughout (the second election does not count as a SNAP, it was expected for when the new constitution is ready)
replace snap = 1 if Type==2 & Country == 3 & Rankleg>1 & Rankleg!=. &  time_since_last_leg < (365.25*2.5) & Year <1956 // Finland, regular term 3 years until 1955
replace snap = 1 if Type==2 & Country == 3 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5) & Year > 1955 // Finland, regular term 4 years from 1955/56
replace snap = 1 if Type==2 & Country == 4 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*4.5) // France, regular term 5 years throughout (since 1946) 
replace snap = 0 if Type == 2 & Country == 4 & Year == 1946 & Round == 1 & Date == "10/11/46" // This was not a snap election (expected after the adoption of the constitution)
replace snap = 0 if Type==2 & Country == 4 & Year == 1967 & Round ==1 // Exception, shorter regular term see here: https://www.assemblee-nationale.fr/dyn/decouvrir-l-assemblee/les-elections-legislatives-depuis-1958 
replace snap = 1 if Type==2 & Country == 5 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5) // Greece, regular term 4 years throughout 
replace snap = 1 if Type==2 & Country == 6 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*4.5) // Ireland, regular term 5 years throughout 
replace snap = 1 if Type==2 & Country == 7 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5) // Iceland, regular term 4 years throughout
replace snap = 1 if Type==2 & Country == 8 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*4.5) // Malta, regular term 5 years throughout
replace snap = 1 if Type==2 & Country == 9 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5) // Netherlands, regular term 4 years throughout
replace snap = 1 if Type==2 & Country == 10 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5) // Norway, regular term 4 years throughout
replace snap = 1 if Type==2 & Country == 11 & Rankleg>2 & Rankleg!=. & time_since_last_leg < (365.25*3.5) // Portugal, regular term 4 years throughout (the second election does not count as SNAP, was scheduled for when the constitution is ready)
replace snap = 1 if Type==2 & Country == 12 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5) & Year < 1971 // Sweden, until 1970 4 years 
replace snap = 1 if Type==2 & Country == 12 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*2.5) & Year > 1970 & Year<1995 // Sweden, until 1970-1994 3 years 
replace snap = 1 if Type==2 & Country == 12 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5) & Year >1995 // Sweden, regular term 4 years since 1994
replace snap = 1 if Type==2 & Country == 21 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5) // Bulgaria, regular term 4 years throughout
replace snap = 1 if Type==2 & Country == 22 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5) // Czech Republic, regular term 4 years throughout
replace snap = 1 if Type==2 & Country == 23 & Rankleg>2 & Rankleg!=. & time_since_last_leg < (365.25*3.5) & Year > 1995 // Estonia, regular term 4 years since 1996 (the first in 1992 and 1995, were not snaps)
replace snap = 1 if Type==2 & Country == 24 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5) // Hungary, regular term 4 years throughout
replace snap = 1 if Type==2 & Country == 25 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5) & Year > 1998 // Latvia, regular term 4 years after 1998 (before 1998, none was snap)
replace snap = 1 if Type==2 & Country == 26 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5)  // Lithuania, regular term 4 years throughout
replace snap = 1 if Type==2 & Country == 27 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5)  // Poland, regular term 4 years throughout
replace snap = 1 if Type==2 & Country == 28 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5)  // Romania, regular term 4 years throughout
replace snap = 1 if Type==2 & Country == 29 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5)  // Slovakia, regular term 4 years throughout
replace snap = 1 if Type==2 & Country == 30 & Rankleg>1 & Rankleg!=. & time_since_last_leg < (365.25*3.5)  // Slovenia, regular term 4 years throughout

bysort Country: fre snap
list Country Year snap Rankleg  time_since_last_leg  if Country== 23 & Type==2
list Country Year snap Rankleg if Country== 24 & Type==2
list Country Year snap Rankleg if Country== 2 & Type==2
list Country Year snap Rankleg if Country== 4 & Type==2
list Country Year snap Rankleg posit_date  time_since_last_leg if Country== 1 & Type==2
list Country Year snap Rankleg posit_date  time_since_last_leg if Country== 7 & Type==2

lab define snap 0 "Regular Election" 1 "Snap Election", modify
lab var snap snap
lab var snap "Snap Election"

*** Checking and rescaling coverage
fre Coverage_pop 
list Country Type Year Coverage_pop Coverage_dis if Coverage_pop ==. 
replace Coverage_pop = Coverage_pop / 100 if Country < 21 // To get shares of the electorate

* Dropping other elections for consistency
drop if Type ==10

*Creating filter for legislative elections that include some second-order elections with missing (and thus estimated values) for population coverage
forvalues i = 1/28 {
gen missing_cov_`i' = 0
replace missing_cov_`i' = 1 if (missing_info_cov ==1 & Timetoleg_`i'< 1826.25 & Timetoleg_`i' > 0) // 1826.25 corresponds to 5 years (5*365.25)
}

forvalues i = 1/28 {
egen missing_`i' = sum(missing_cov_`i'), by(Country)
}


gen filter_coverage = .
la var filter_coverage  "Filter for population coverage"
forvalues i = 1/28 {
	replace filter_coverage = missing_`i' if Rankleg==`i'
	}
fre filter_coverage
list Country Year if filter_coverage>0 & filter_coverage!=. // Good, these elections will be excluded in a robustness check. 



****************************************************************
**************** Measures of Election Frequency ****************
****************************************************************


********* Time since last leg election ************ 
gen legislative_1st_round = legislative == 1 & Round ==1 

sort Country legislative_1st_round posit_date
gen last_leg_days = .
replace last_leg_days = posit_date-posit_date[_n-1] if Country == Country[_n-1] & legislative_1st_round==1 & legislative_1st_round[_n-1]==1

sort Country date // Sort back		 
 

 
****** Type 0: Number of Elections ******

forvalues i = 1/28 {
gen num_el_5_cont_`i' = .
replace num_el_5_cont_`i' = Coverage_pop if Timetoleg_`i'< 1826.25 & Timetoleg_`i' >= 1461 // 1826.25 corresponds to 5 years
}
forvalues i = 1/28 {
gen num_el_4_cont_`i' = .
replace num_el_4_cont_`i' =  Coverage_pop if Timetoleg_`i'< 1461 & Timetoleg_`i' >= 1095.75 // 1461 corresponds to 4 years
}
forvalues i = 1/28 {
gen num_el_3_cont_`i' = .
replace num_el_3_cont_`i' =  Coverage_pop if Timetoleg_`i'< 1095.75 & Timetoleg_`i' >=  730.5 // 1095.75 corresponds to 3 years
}
forvalues i = 1/28 {
gen num_el_2_cont_`i' = .
replace num_el_2_cont_`i' =  Coverage_pop if Timetoleg_`i'< 730.5 & Timetoleg_`i' >= 365.25 // 730.5 corresponds to 2 years
}
forvalues i = 1/28 {
gen num_el_1_cont_`i' = .
replace num_el_1_cont_`i' =  Coverage_pop if Timetoleg_`i'< 365.25 & Timetoleg_`i' >= 182.625  // 365.25 corresponds to 1 year 
}
forvalues i = 1/28 {
gen num_el_0_5_cont_`i' = .
replace num_el_0_5_cont_`i' =  Coverage_pop if Timetoleg_`i'< 182.625 & Timetoleg_`i' >= 36 // 182.625 corresponds to 0.5 year 
}

forvalues i = 1/28 {
gen num_el_0_35d_cont_`i' = .
replace num_el_0_35d_cont_`i' =  Coverage_pop if Timetoleg_`i'< 36 & Timetoleg_`i' > 0 // 35 days (5 weeks)
}



*Aggregating Contributions

foreach lname in num_el_5  num_el_4  num_el_3 num_el_2  num_el_1  num_el_0_5 num_el_0_35d {
forvalues i = 1/28 {
egen `lname'_`i' = sum(`lname'_cont_`i'), by(Country)
}
}

*Generating Indexes

local beg 0
foreach lname in 0_35d 0_5 1 2 3 4 5 {
gen index_nofel_`lname' = .
la var index_nofel_`lname'  "Number of Elections: `beg' - `lname' years"
local beg `lname'
forvalues i = 1/28 {
	replace index_nofel_`lname' = num_el_`lname'_`i' if Rankleg==`i'
	}
	}
  
lab var index_nofel_0_5 "Number of Elections: 5 weeks - 0.5 years"
lab var index_nofel_0_35d "Number of Elections: 5 weeks" 
  
cap gen index_nofel_3_4 = index_nofel_3 + index_nofel_4
cap gen index_nofel_1_2 = index_nofel_1 + index_nofel_2  
lab var index_nofel_0_5  "Number of Elections: 0.1-0.5 years"
lab var index_nofel_1_2 "Number of Elections: 0.5-2 years"
lab var index_nofel_3_4 "Number of Elections: 2-4 years"
lab var index_nofel_5 "Number of Elections: 4-5 years"  
  
 
********* Type 1: only share of electorate concerned ************ 


forvalues i = 1/28 {
gen no_weight_5_cont_`i' = .
replace no_weight_5_cont_`i' = Coverage_pop/exp(Timetoleg_`i'/365.25) if Timetoleg_`i'< 1826.25 & Timetoleg_`i' > 0 // 1826.25 corresponds to 5 years
}


list Country Year no_weight_5_cont_20 if Year>2010 & Country==4


*Aggregating Contributions

foreach lname in   no_weight_5  {
forvalues i = 1/28 {
egen `lname'_`i' = sum(`lname'_cont_`i'), by(Country)
}
}


list Country Year no_weight_5_cont_20 if Year>2010 & Country==4
list Country Year no_weight_5_20 if Year>2010 & Country==4 // ok 


*Generating Indexes

foreach lname in 5 {
gen index_n_w_`lname' = .
la var index_n_w_`lname'  "IEF"
forvalues i = 1/28 {
	replace index_n_w_`lname' = no_weight_`lname'_`i' if Rankleg==`i'
	}
	}

	
	
 ****** Type 2: Weight of the election ******

tabstat Turnout if Round==1, by(Type)
preserve 
collapse Turnout if Round==1, by(Type)
tabstat Turnout, by(Type)
su Turnout if Type ==2
gen Turnout_ratio = Turnout/r(mean) 
tabstat Turnout_ratio, by(Type)
restore 
 
recode Type (2 = 1) (8 1=0.9) (6 7 = 0.8) (3 4 = 0.7) (5=0.6) (else=.), gen(weight)


forvalues i = 1/28 {
gen weight_5_cont_`i' = .
replace weight_5_cont_`i' = Coverage_pop*weight/exp(Timetoleg_`i'/365.25) if Timetoleg_`i'< 1826.25 & Timetoleg_`i' > 0 // 1826.25 corresponds to 5 years
}

*Aggregating Contributions

foreach lname in  weight_5 {
forvalues i = 1/28 {
egen `lname'_`i' = sum(`lname'_cont_`i'), by(Country)
}
}

*Generating Indexes

foreach lname in 5 {
gen index_w_`lname' = .
la var index_w_`lname'  "IEF: Weight"
forvalues i = 1/28 {
	replace index_w_`lname' = weight_`lname'_`i' if Rankleg==`i'
	}
	}


 ****** Type 2b: Weight of the election (data driven) ******

recode Type (1 2 = 1) (else=0.69), gen(weight2) // Alternative weight, based on the regression coefficients from Model 5 in Table 1 in the manuscript. 



forvalues i = 1/28 {
gen weight_5b_cont_`i' = .
replace weight_5b_cont_`i' = Coverage_pop*weight2/exp(Timetoleg_`i'/365.25) if Timetoleg_`i'< 1826.25 & Timetoleg_`i' > 0 // 1826.25 corresponds to 5 years
}

*Aggregating Contributions

foreach lname in  weight_5b {
forvalues i = 1/28 {
egen `lname'_`i' = sum(`lname'_cont_`i'), by(Country)
}
}

*Generating Indexes

foreach lname in 5b {
gen index_w_`lname' = .
la var index_w_`lname'  "IEF: Weight (alt.)"
forvalues i = 1/28 {
	replace index_w_`lname' = weight_`lname'_`i' if Rankleg==`i'
	}
	}
	
	
	
****** Type 4: First Order Elections (only) ******
	

forvalues i = 1/28 {
gen no_weight_FOE_5_cont_`i' = .
replace no_weight_FOE_5_cont_`i' = Coverage_pop/exp(Timetoleg_`i'/365.25) if Timetoleg_`i'< 1826.25 & Timetoleg_`i' > 0 & inlist(Type, 1, 2) // 1826.25 corresponds to 5 years
}

*Aggregating Contributions

foreach lname in no_weight_FOE_5  {
forvalues i = 1/28 {
egen `lname'_`i' = sum(`lname'_cont_`i'), by(Country)
}
}

*Generating Indexes

foreach lname in 5 {
gen  index_n_w_FOE_`lname' = .
la var  index_n_w_FOE_`lname'  "IEF: First-Order Elections"
forvalues i = 1/28 {
	replace index_n_w_FOE_`lname' = no_weight_FOE_`lname'_`i' if Rankleg==`i'
	}
	}
	

****** Type 4: Second Order Elections (only) ******
	

forvalues i = 1/28 {
gen no_weight_SOE_5_cont_`i' = .
replace no_weight_SOE_5_cont_`i' = Coverage_pop/exp(Timetoleg_`i'/365.25) if Timetoleg_`i'< 1826.25 & Timetoleg_`i' > 0 & Type!=1 & Type!=2  // 1826.25 corresponds to 5 years
}



*Aggregating Contributions

foreach lname in  no_weight_SOE_5  {
forvalues i = 1/28 {
egen `lname'_`i' = sum(`lname'_cont_`i'), by(Country)
}
}

*Generating Indexes

foreach lname in 5 {
gen  index_n_w_SOE_`lname' = .
la var  index_n_w_SOE_`lname'  "IEF: Second-Order Elections"
forvalues i = 1/28 {
	replace index_n_w_SOE_`lname' = no_weight_SOE_`lname'_`i' if Rankleg==`i'
	}
	}	
	
****** Type 5: By Election Type - 5 Years (only) ******	


forvalues y = 1/8 {
forvalues i = 1/28 {
gen no_weight_T0`y'_5_cont_`i' = .
replace no_weight_T0`y'_5_cont_`i' = Coverage_pop/exp(Timetoleg_`i'/365.25) if Timetoleg_`i'< 1826.25 & Timetoleg_`i' > 0 & Type==`y' // 1826.25 corresponds to 5 years (5*365.25)
}
}

*Aggregating Contributions
forvalues y = 1/8 {
forvalues i = 1/28 {
egen no_weight_T0`y'_`i' = sum(no_weight_T0`y'_5_cont_`i'), by(Country)
}
}

*Generating Indexes
	
forvalues y = 1/8 {
gen  index_n_w_T0`y'_5 = .
local type:  label Type `y' 
la var  index_n_w_T0`y'_5 "IEF `type'"
forvalues i = 1/28 {
	replace index_n_w_T0`y'_5 = no_weight_T0`y'_`i' if Rankleg==`i'
	}
	}		
	
	
*Regrouping Elections groups
gen index_n_w_national_5 = index_n_w_T01_5+index_n_w_T02_5+index_n_w_T03_5
lab var index_n_w_national_5 "IEF: National Elections"
gen index_n_w_referendum_5 = index_n_w_T04_5 
lab var index_n_w_referendum_5 "IEF: Referendums" 
gen index_n_w_supranational_5 = index_n_w_T05_5  
lab var index_n_w_supranational_5 "IEF: Supranational Elections" 
gen index_n_w_subnational_5 = index_n_w_T06_5+index_n_w_T07_5+index_n_w_T08_5
lab var index_n_w_subnational_5 "IEF: Subnational Elections" 
	
drop no_weight* weight* num_el* 

save electionsdata_4.dta, replace
 
 
****************************************************************************************
*********************************** For Micro Analysis **********************************
*****************************************************************************************

cd `"${beg_path}Data\Analysis 1\"'

use electionsdata_4.dta, clear

gen Majority = abs(50-First)
lab var  Majority  "Majority Status"
gen Closeness = First-Second
lab var Closeness "Closeness"
gen CV = 0
replace CV = 1 if (Country == 9 & Year<= 1967 & Type==2) | (Country == 5 & Year<=2000 & Type==2)


keep if Type ==2 & Round ==1
keep Year index_n_w_5 snap Country Date date Turnout Majority Closeness  Elsystem CV  Joint1 


*Creating country codes for the merge: 

gen Country_CSES = . 
replace Country_CSES = 13 if  Country == 	1	//	DK
replace Country_CSES = 47 if  Country ==	2	 //	ES
replace Country_CSES = 15 if  Country == 	3	//	FI
replace Country_CSES = 16 if  Country == 	4	//	FR
replace Country_CSES = 19 if  Country ==	5	//	GR
replace Country_CSES = 23 if  Country ==	6	//	IE
replace Country_CSES = 22 if  Country ==	7	//	IS
replace Country_CSES = . if   Country ==	8	//	MT
replace Country_CSES = 33 if  Country ==	9	//	NL
replace Country_CSES = 35 if  Country ==	10	//	NO
replace Country_CSES = 39 if  Country ==	11	//	PT
replace Country_CSES = 48 if  Country == 	12	//	SE
replace Country_CSES = 8 if  Country == 	21	//	Bulgaria
replace Country_CSES = 12 if  Country == 	22	//	Czech Republic
replace Country_CSES = 14 if  Country == 	23	//	Estonia
replace Country_CSES = 21 if  Country == 	24	//	Hungary
replace Country_CSES = 29 if  Country == 	25	//	Latvia
replace Country_CSES = 30 if  Country == 	26	//	Lithuania
replace Country_CSES = 38 if  Country == 	27	//	Poland
replace Country_CSES = 41 if  Country == 	28	//	Romania
replace Country_CSES = 44 if  Country == 	29	//	Slovakia
replace Country_CSES = 45 if  Country == 	30	//	Slovenia
drop if Country_CSES == . 


replace date = date -1 if Country_CSES == 12 | (Country_CSES == 35 & Year  == 2009) | (Country_CSES == 35 & Year  == 2013)


save `"${beg_path}Data\Analysis 3\cses_fre_to_merge.dta"', replace 
